/*******************************************************************************
 * Copyright 2011 Andrea Guitto
 * 
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * 
 *   http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 ******************************************************************************/
package com.andreaguitto.mobile.android.framework.base.workers;

import java.util.ArrayList;
import java.util.Hashtable;
import java.util.List;

import android.os.Bundle;

import com.andreaguitto.mobile.android.framework.base.interfaces.AGActivityLayerInterface;
import com.andreaguitto.mobile.android.framework.base.interfaces.activities.AGActivityLayerCompatibleInterface;

public class AGActivityLayerCompatibleManager {

	private static List<AGActivityLayerInterface> initActivityLayerList(){
		return new ArrayList<AGActivityLayerInterface>();
	}	
	
	public static List<AGActivityLayerInterface> manageAddActivityLayer(AGActivityLayerCompatibleInterface activity, List<AGActivityLayerInterface> filtersRegistered, AGActivityLayerInterface filter){
		if(filtersRegistered==null){
			filtersRegistered = initActivityLayerList();
		}
		
		if(!filtersRegistered.contains(filter)){
			filtersRegistered.add(filter);
		}
		filter.onLayerAdded();
		return filtersRegistered;
	}
	
	public static List<AGActivityLayerInterface> manageRemoveActivityLayer(AGActivityLayerCompatibleInterface activity, List<AGActivityLayerInterface> filtersRegistered, AGActivityLayerInterface filter){
		if(filtersRegistered!=null){		
			if(!filtersRegistered.contains(filter))
				filtersRegistered.remove(filter);
		}
		filter.onLayerRemoving();
		return filtersRegistered;
	}
	
	public static List<AGActivityLayerInterface> manageClearActivityLayers(AGActivityLayerCompatibleInterface activity, List<AGActivityLayerInterface> filtersRegistered){
		if(filtersRegistered!=null){
			for(AGActivityLayerInterface filter : filtersRegistered){
				filter.onLayerRemoving();
				filtersRegistered.remove(filter);
			}
		}
		
		filtersRegistered=null;
		return filtersRegistered;
	}
	
	public static void manageOnSaveInstanceState(AGActivityLayerCompatibleInterface activity, List<AGActivityLayerInterface> filtersRegistered, Bundle savedInstanceState){
		if(filtersRegistered!=null){	
			for(AGActivityLayerInterface filter : filtersRegistered){
				activity.onBeforeLayerAction(filter, AGActivityLayerInterface.ACTION_SAVE_INSTANCE);
				filter.onSaveInstanceState(savedInstanceState);
				activity.onAfterLayerAction(filter, AGActivityLayerInterface.ACTION_SAVE_INSTANCE);
			}
		}
	} 

	public static void manageOnRestoreInstanceState(AGActivityLayerCompatibleInterface activity, List<AGActivityLayerInterface> filtersRegistered, Bundle savedInstanceState){
		if(filtersRegistered!=null){	
			for(AGActivityLayerInterface filter : filtersRegistered){
				activity.onBeforeLayerAction(filter, AGActivityLayerInterface.ACTION_RESTORE_INSTANCE);
				filter.onRestoreInstanceState(savedInstanceState);
				activity.onAfterLayerAction(filter, AGActivityLayerInterface.ACTION_RESTORE_INSTANCE);
			}
		}
	}	
	
	public static void manageOnCreate(AGActivityLayerCompatibleInterface activity, List<AGActivityLayerInterface> filtersRegistered, Bundle savedInstanceState){
		if(filtersRegistered!=null){	
			for(AGActivityLayerInterface filter : filtersRegistered){
				activity.onBeforeLayerAction(filter, AGActivityLayerInterface.ACTION_CREATE);
				filter.onCreate(savedInstanceState);
				activity.onAfterLayerAction(filter, AGActivityLayerInterface.ACTION_CREATE);
			}
		}
	}
	
	public static void manageOnStart(AGActivityLayerCompatibleInterface activity, List<AGActivityLayerInterface> filtersRegistered){
		if(filtersRegistered!=null){
			for(AGActivityLayerInterface filter : filtersRegistered){
				activity.onBeforeLayerAction(filter, AGActivityLayerInterface.ACTION_START);
				filter.onStart();
				activity.onAfterLayerAction(filter, AGActivityLayerInterface.ACTION_START);
			}
		}		
	}
	
	public static void manageOnResume(AGActivityLayerCompatibleInterface activity, List<AGActivityLayerInterface> filtersRegistered){
		if(filtersRegistered!=null){
			for(AGActivityLayerInterface filter : filtersRegistered){
				activity.onBeforeLayerAction(filter, AGActivityLayerInterface.ACTION_RESUME);
				filter.onResume();
				activity.onAfterLayerAction(filter, AGActivityLayerInterface.ACTION_RESUME);
			}
		}		
	}
	
	public static void manageOnPause(AGActivityLayerCompatibleInterface activity, List<AGActivityLayerInterface> filtersRegistered){
		if(filtersRegistered!=null){
			for(AGActivityLayerInterface filter : filtersRegistered){
				activity.onBeforeLayerAction(filter, AGActivityLayerInterface.ACTION_PAUSE);
				filter.onPause();
				activity.onAfterLayerAction(filter, AGActivityLayerInterface.ACTION_PAUSE);
			}
		}		
	}	
	
	public static void manageOnStop(AGActivityLayerCompatibleInterface activity, List<AGActivityLayerInterface> filtersRegistered){
		if(filtersRegistered!=null){
			for(AGActivityLayerInterface filter : filtersRegistered){
				activity.onBeforeLayerAction(filter, AGActivityLayerInterface.ACTION_STOP);
				filter.onStop();
				activity.onAfterLayerAction(filter, AGActivityLayerInterface.ACTION_STOP);
			}		
		}		
	}	
	
	public static void manageOnDestroy(AGActivityLayerCompatibleInterface activity, List<AGActivityLayerInterface> filtersRegistered){
		if(filtersRegistered!=null){
			for(AGActivityLayerInterface filter : filtersRegistered){
				activity.onBeforeLayerAction(filter, AGActivityLayerInterface.ACTION_DESTROY);
				filter.onDestroy();
				activity.onAfterLayerAction(filter, AGActivityLayerInterface.ACTION_DESTROY);
			}
		}		
	}
	
	public static Hashtable<String,Object> initSharedObjects(AGActivityLayerCompatibleInterface activity){
		return new Hashtable<String, Object>();
	}	
}
